<?php
/**
 * Description of sale
 * 
 * @author hoangdung
 * @since Feb 14, 2014 - 3:11:15 PM
 * @ edit by Hai.Dang August - 2014
 */
?>
<?php
$baseUrl = Yii::app()->baseUrl;
$cs      = Yii::app()->getClientScript();
$cs->registerScriptFile($this->assetDirWeb . 'js/Webmaster/perfectscrollbar/jquery.mousewheel.js');
$cs->registerScriptFile($this->assetDirWeb . 'js/Webmaster/perfectscrollbar/perfect-scrollbar.js');
$cs->registerCssFile($this->assetDirWeb . 'js/Webmaster/perfectscrollbar/perfect-scrollbar.css');
$cs->registerScriptFile($this->assetDirWeb . 'js/Webmaster/jchart.js');
$cs->registerCssFile($this->assetDirWeb . 'css/Webmaster/jchart.css');
?>
<link type="text/css" rel="stylesheet" href="<?php echo $this->assetDirWeb ?>css/Webmaster/appointment.css" />
<style>
    #list_events{
        overflow: hidden;
        height:500px;
        position:relative;
    }
    #tbl_header{
        position:absolute;
        left:0px;
        z-index:900;
    }
    #tbl_header2{
        position:absolute;
        left:0px;
        z-index:1100;
    }
    #list_events th{
        min-width: 90px;
    }
    tr.total{
        background-color: #FFFFCC;
        font-weight: bold;
    }
    #business_filter_chzn{
        width: 200px!important;
    }
    .chzn-drop{
        width: 198px!important;
    }
    .chzn-search input{
        width: 163px!important;
    }
    .view_row.active{
        background:#FFFFCC;
    }
    .view_row2.active{
        background:#FFFFCC;
    }
    .view_row{
        cursor:pointer;
    }
    tr.view_row:hover {
        background-color: #FFFFCC;
    }
    .summary-table .header{
        font-weight:bold;
    }
    #ui-datepicker-div{
        z-index:1000 !important;
    }
    .ps-container .ps-scrollbar-x-rail{
        z-index:10000;
    }
</style>
<div class="row-fluid">
    <div class="box span12">
        <div class="box-header" data-original-title>
            <h2><i class="icon-table"></i><span class="break"></span>Report Sale</h2>
        </div>
        <div class="box-content">
            <div class="" >
                <div id="jqChart" style="width: 100%; height: 300px;margin-bottom:40px">
                    <div class="clearfix"></div>
                </div>
                <div>
                    <form method="GET" id="report_filter" onsubmit="return true;" >
                        <div class="pull-left">
                            <fieldset>
                                <div class="control-group">
                                    <div class="controls">
                                    </div>
                                </div>
                            </fieldset>
                        </div>
                        <div id="filter_form" class="pull-left">
                            <?php
                            if ($this->isAdmin)
                                echo CHtml::dropDownList('business', array(), array(), array(
                                    'onchange' => 'load_select_staff();',
                                    'style'    => 'width:200px; max-width:200px',
                                ));
                            ?>
                            &nbsp;
                            <?php if (!$this->isStaff): ?>
                                <?php
                                $staff_array    = array();
                                $staff_array[0] = 'Select Staff';
                                if ($this->isBussiness)
                                {
                                    $curr_user   = Yii::app()->user->currentUser;
                                    $staff_array = $staff_array + Users::getAllStaffByBusiness($curr_user->ukey, TRUE);
                                }
                                ?>
                                <?php echo CHtml::dropDownList('staff', ((isset($_GET['staff'])) ? array($_GET['staff']) : array()), $staff_array, array()); ?>
                            <?php endif; ?>
                        </div>
                        <div class="date-filter" style="width:410px">
                            <input type="text" id="le_start_date" name="start_date" class="" readonly="" placeholder="Start date" value="<?= (isset($_GET['start_date']) && trim($_GET['start_date']) != "") ? $_GET['start_date'] : "" ?>" />
                            <input type="text" id="le_end_date" name="end_date" class="" readonly="" placeholder="End date" value="<?= (isset($_GET['end_date']) && trim($_GET['end_date']) != "") ? $_GET['end_date'] : "" ?>" />
                            <button class="btn btn-success" id="btn-filter-submit" type="submit" style="margin:0px;margin-bottom: 10px;">Submit</button>
                            <button class="btn btn-warning" id="btn-clear" type="button" style="margin:0px;margin-bottom: 10px;">Clear</button>
                            <!--<input type="hidden" name="page" value="<?= (isset($_GET['page'])) ? $_GET['page'] : 1 ?>" />-->
                        </div>
                    </form>
                    <div class="pull-right">
                        <button class="btn pull-left" id="btnprint">
                            Print 
                            <i class="icon-print"></i>
                        </button>&nbsp;
                        <form  class="pull-right" id="frm_export" action="<?php echo $this->createUrl('report/Export'); ?>" method="POST">
                            <input type="hidden" name="le_start_date" id="start_date" value="" />
                            <input type="hidden" name="le_end_date" id="end_date" value="" />
                            <?php if (!$this->isStaff): ?>
                                <input type="hidden" name="biz_ukey" id="biz_ukey" value="" >
                                <input type="hidden" name="staff_ukey" id="staff_ukey" value="" >
                            <?php endif; ?>
                        </form>
                        <button class="btn btn-info" id="export_form1">
                            Export 
                            <i class="icon-download"></i>
                        </button>
                    </div>
                    <div class="clearfix"></div>
                </div>

                <div>
                    <div class="table-responsive">
                        <table class="table table-bordered bootstrap-datatable event-list summary-table">
                            <thead>
                                <tr>
                                    <th colspan="11">Summary</th>
                                </tr>
                            </thead>  
                            <tbody>
                                <tr class="header">
                                    <td>TOTAL</td>
                                    <td>Price</td>
                                    <td>GST</td>
                                    <td>Discount</td>
                                    <td>Cash</td>
                                    <td>Paypal</td>
                                    <td>Credit Card</td>
                                    <td>NETS</td>
                                    <td>Package sessions used</td>
                                    <td>Package Revenue</td>
                                    <td>Retail Revenue</td>
                                    <td>Retail Discount</td>
                                </tr>
                                <tr>
                                    <td id="total_price" style="font-weight:bold; color:#F30">$<?= (isset($totalOrder)) ? number_format(($totalOrder), 3, ".", ",") : 0 ?></td>
                                    <td id="sum_price" style="font-weight:bold">$<?= (isset($totalPrice)) ? number_format($totalPrice, 3, ".", ",") : 0 ?></td>
                                    <td id="sum_gst" style="font-weight:bold">$<?= (isset($totalTax)) ? number_format($totalTax, 3, ".", ",") : 0 ?></td>
                                    <td id="sum_dis" style="font-weight:bold">$<?= (isset($totalDiscount)) ? number_format(($totalDiscount), 3, ".", ",") : 0 ?></td>
                                    <td id="sum_cas">$<?= (isset($totalCash)) ? number_format(($totalCash), 3, ".", ",") : 0 ?></td>
                                    <td id="sum_paypal">$ <?= (isset($totalPaypal)) ? number_format(($totalPaypal), 3, ".", ",") : 0 ?></td>
                                    <td id="sum_cc">$<?= (isset($totalCredit)) ? number_format(($totalCredit), 3, ".", ",") : 0 ?></td>
                                    <td id="sum_net"></td>
                                    <td id="sum_package_ses"><?= (isset($totalPackageSession)) ? number_format(($totalPackageSession), 0, ".", ",") : 0 ?></td>
                                    <td id="sum_package_revenue">$<?= (isset($totalPackage)) ? number_format(($totalPackage), 3, ".", ",") : 0 ?></td>
                                    <td id="sum_reatail_revenue" style="font-weight:bold">$<?= (isset($totalRetail)) ? number_format(($totalRetail), 3, ".", ",") : 0 ?></td>
                                    <td id="sum_retail_dis" style="font-weight:bold">$<?= (isset($totalDiscountRetail)) ? number_format(($totalDiscountRetail), 3, ".", ",") : 0 ?></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>  
                    <div class="clearfix"></div>
                </div>
                <div class="clearfix"></div>
                <div id="list_events" class="table-responsive">
                    <table class="table table-bordered bootstrap-datatable event-list" id="tbl_header" style="display:none">
                        <thead >
                            <tr>
                                <th style="width:50px !important; min-width:50px !important">#</th>
                                <th>Order Date</th>
                                <th>Apt Date</th>
                                <th>Receipt Date</th>
                                <th>Service Name</th>
                                <th>Customer</th>
                                <th>Merchant</th>
                                <th>Price</th>
                                <th>GST</th>
                                <th>Discount</th>
                                <th>Retail Revenue</th>
                                <th>Retail Discount</th>
                                <th>Total Order</th>
                                <th>Cash</th>
                                <th>Paypal</th>
                                <th>Credit Card</th>
                                <th>NETS</th>
                                <th>Package sessions used</th>
                                <th>Package Revenue</th>
                                <th>Person in Charge</th>
                                <th>Branch</th>
                            </tr>
                        </thead> 
                    </table>
                    <table class="table table-bordered bootstrap-datatable event-list" id="tbl_header2" style="display:none">
                        <thead >
                            <tr>
                                <th class="col_date_td" style="border-bottom-right-radius:0px;webkit-border-bottom-right-radius: 0px">Order Date</th>
                                <th class="col_apt_date_td" style="border-bottom-right-radius:0px;webkit-border-bottom-right-radius: 0px">Apt Date</th>
                            </tr>
                        </thead> 
                    </table>
                    <table class="table table-bordered bootstrap-datatable event-list" id="tbl_date_col" style="display:none; position:absolute; top:0px; left:0px;z-index:1000; background:#FFF">
                        <tr class="tbl_date_col_tr">
                            <th style="border-bottom-right-radius:0px;webkit-border-bottom-right-radius: 0px">Order Date</th>
                            <th style="border-bottom-right-radius:0px;webkit-border-bottom-right-radius: 0px">Apt Date</th>
                        </tr>
                    </table>
                    <div id="Table_event" style="position:relative">
                        <div class="table-responsive">	
                            <table class="table table-bordered bootstrap-datatable event-list">
                                <thead>
                                    <tr id="header_tr">
                                        <th style="width:50px !important; min-width:50px !important">#</th>
                                        <th class="col_date">Order Date</th>
                                        <th class="col_apt_date">Apt Date</th>
                                        <th>Receipt Date</th>
                                        <th>Service Name</th>
                                        <th>Customer</th>
                                        <th>Merchant</th>      
                                        <th>Price</th>
                                        <th>GST</th>
                                        <th>Discount</th>
                                        <th>Retail Revenue</th>
                                        <th>Retail Discount</th>
                                        <th>Total Order</th>
                                        <th>Cash</th>
                                        <th>Paypal</th>
                                        <th>Credit Card</th>
                                        <th>NETS</th>
                                        <th>Package sessions used</th>
                                        <th>Package Revenue</th>
                                        <th>Person in Charge</th>
                                        <th>Branch</th>
                                    </tr>
                                </thead>  
                                <tbody>
                                    <?php
                                    $total_price           = 0;
                                    $total_discount        = 0;
                                    $total_tax             = 0;
                                    $total_cash            = 0;
                                    $total_paypal          = 0;
                                    $total_credit          = 0;
                                    $total_package_session = 0;
                                    $total_package         = 0;
                                    $total_retail          = 0;
                                    $total_discount_retail = 0;
                                    $total_order           = 0;
                                    $date_col              = "";
                                    ?>
                                    <?php
                                    foreach ($receipts as $i => $v):
                                        if (isset($v->order) && !empty($v->order)):
                                            $date_col.='<tr class="' . $v->id . '_td view_row2"><td class="col_date_td">' . date('d/M/Y', @$v->order->order_date) . '</td><td class="col_apt_date_td">' . date('d/M/Y', @$v->order->appoint->date) . '</td></tr>';
                                            ?>
                                            <tr class="view_row" data-rel="<?= $v->id ?>">
                                                <td><?php echo $i + 1 ?></td>
                                                <td><?php echo date('d/M/Y', @$v->order->order_date) ?></td>

                                                <td class="col_date"><?php echo date('d/M/Y', @$v->order->appoint->date) ?></td>
                                                <td><?php echo date('d/M/Y', @$v->receipt_date) ?></td>
                                                <td>
                                                    <?php if (isset($v->order->service)): ?>
                                                        <a target="_blank" href="<?php echo @$v->order->service->editUrl ?>">
                                                            <?php echo @$v->order->service->s_name ?>
                                                        </a>
                                                    <?php endif; ?>
                                                </td>
                                                <td><!--Customer-->
                                                    <?php if (isset($v->order->customer->name)): ?>
                                                        <a target="_blank" href="<?php echo $v->order->customer->viewUrl ?>">
                                                            <strong><?php echo $v->order->customer->getName(); ?> </strong>
                                                        </a><br />
                                                        <?php echo @$v->order->customer->user->ukey ?><br />
                                                        <?php echo @$v->order->customer->user->email ?>
                                                    <?php endif; ?>
                                                </td>

                                                <!--Root Merchant-->
                                                <td>
                                                    <?php if (@$v->order->merchant): ?>
                                                        <?php $cur_merchant = @$v->order->merchant->isBranch ? $v->order->merchant->author : $v->order->merchant; ?>
                                                    <?php else: ?>
                                                        <?php $cur_merchant = @$v->order->service->user; ?>
                                                        <!--End Root Merchant-->
                                                    <?php endif; ?>
                                                    <?php if (isset($cur_merchant)): ?>
                                                        <?php if ($this->isAdmin): ?>
                                                            <a target="_blank" href="<?php echo $cur_merchant->viewUrl ?>">
                                                                <?php echo $cur_merchant->getName(); ?> 
                                                            </a>
                                                        <?php else: ?>
                                                            <?php echo $cur_merchant->getName(); ?> 
                                                        <?php endif; ?>
                                                    <?php endif; ?>
                                                </td>


                                                <td>$<?php echo @$v->order->s_price ?></td>

                                                <!--Tax-->
                                                <td><?= (isset($v->tax) && (float) $v->tax > 0) ? '$' . $v->tax . ' (' . @$v->order->tax . '%)' : 'included' ?>
                                                </td>
                                                <!--End Tax-->

                                                <!--Discount-->
                                                <td>$<?php
                                                    $discount                = @$v->discount_type == '$' ? $v->discount : @$v->order->s_price * $v->discount / 100;
                                                    echo $discount
                                                    ?>
                                                </td>
                                                <!--Discount-->

                                                <!--Retail Revenue-->
                                                <td><?php
                                                    $retail_receipt_price    = 0;
                                                    $retail_receipt_discount = 0;
                                                    if (@$v->order->retailOrder && $v->order->retailOrder->receipt)
                                                    {
                                                        $retail_receipt_price    = $v->order->retailOrder->receipt->final_price;
                                                        $retail_receipt_discount = $v->order->retailOrder->receipt->discount_type == '%' ?
                                                                $v->order->retailOrder->total_price * $v->order->retailOrder->receipt->discount :
                                                                $v->order->retailOrder->receipt->discount;
                                                    }
                                                    echo $retail_receipt_price != 0 ? '$' . $retail_receipt_price : '';
                                                    ?>
                                                </td>
                                                <!--End Retail Revenue-->

                                                <!--Retail Discount-->
                                                <td>

                                                    <?php echo $retail_receipt_discount != 0 ? '$' : $retail_receipt_discount ?>
                                                </td>
                                                <!--End Retail Discount-->
                                                <!--Total Order-->
                                                <td>
                                                    <strong>$<?= (@$v->order->s_price - $discount + @$v->tax + $retail_receipt_price - $retail_receipt_discount) ?></strong>
                                                </td>
                                                <!--End of Total Order-->
                                                <td><?php echo @$v->order->payment_method == Orders::PAYMENT_CASH ? '$' . @$v->final_price : '' ?></td>
                                                <td><?php echo @$v->order->payment_method == Orders::PAYMENT_PAYPAL ? '$' . @$v->final_price : '' ?></td>
                                                <td><?php echo @$v->order->payment_method == Orders::PAYMENT_CREDIT ? '$' . @$v->final_price : '' ?></td>
                                                <td></td>
                                                <td><?php echo @$v->order->packageOrder ? '1' : '' ?></td>

                                                <!--Package Revenue-->
                                                <td><?php
                                                    if (@$v->order->packageOrder)
                                                    {
                                                        $pack         = $v->order->packageOrder->packageCustomer->package;
                                                        $pack_revenue = $pack->price / $pack->limit;
                                                        echo '$' . $pack_revenue;
                                                    }
                                                    else
                                                    {
                                                        echo '';
                                                    }
                                                    ?>
                                                </td>
                                                <!--End Package Revenue-->

                                                <td><?php echo @$v->order->staffsPerform ?></td>

                                                <!--Branch-->
                                                <td>
                                                    <?php $cur_merchant = @$v->order->merchant->isBranch ? $v->order->merchant : ''; ?>
                                                    <?php if (isset($cur_merchant) && !empty($cur_merchant)): ?>
                                                        <?php if ($this->isAdmin): ?>
                                                            <a target="_blank" href="<?php echo $cur_merchant->viewUrl ?>">
                                                                <?php echo $cur_merchant->getName(); ?> 
                                                            </a>
                                                        <?php endif; ?>
                                                    <?php endif; ?>
                                                </td>
                                                <!--End Branch-->
                                            </tr>
                                            <!--Count Total-->
                                            <?php
                                            $total_price+=@$v->order->s_price;
                                            $total_discount+=$discount;
                                            $total_tax+=@$v->tax;
                                            $total_cash += @$v->order->payment_method == Orders::PAYMENT_CASH ? @$v->order->service->price + @$gst : 0;
                                            $total_paypal += @$v->order->payment_method == Orders::PAYMENT_PAYPAL ? @$v->order->service->price + @$gst : 0;
                                            $total_credit += @$v->order->payment_method == Orders::PAYMENT_CREDIT ? @$v->order->service->price + @$gst : 0;
                                            $total_package_session += @$v->order->packageOrder ? 1 : 0;
                                            $total_package += @$v->order->packageOrder ? $pack_revenue : 0;
                                            $total_retail += $retail_receipt_price;
                                            $total_discount_retail += $retail_receipt_discount;
                                            $total_order +=(@$v->order->s_price - $discount + @$v->tax + $retail_receipt_price - $retail_receipt_discount);
                                            ?>
                                            <!--End Count Total-->
                                        <?php endif;
                                    endforeach;
                                    ?>
                                    <tr class="total">
                                        <td colspan="7">
                                            Total:
                                        </td>
                                        <td>
                                            $<?php echo $total_price; ?>
                                        </td>
                                        <td>
                                            $<?php echo $total_tax; ?>
                                        </td>
                                        <td>
                                            $<?php echo $total_discount; ?>
                                        </td>
                                        <td>
                                            $<?php echo $total_retail; ?>
                                        </td>
                                        <td >
                                            $<?php echo $total_discount_retail; ?>
                                        </td>
                                        <td>
                                            $<?php echo $total_order; ?>
                                        </td>
                                        <td>
                                            $<?php echo $total_cash; ?>
                                        </td>
                                        <td>
                                            $<?php echo $total_paypal; ?>
                                        </td>
                                        <td >
                                            $<?php echo $total_credit; ?>
                                        </td>
                                        <td></td>
                                        <td>
                                            $<?php echo $total_package; ?>
                                        </td>
                                        <td>
<?php echo $total_package_session; ?>
                                        </td>
                                        <td></td>
                                        <td></td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="pagination pagination-centered">
                        <?php
                        $this->widget('application.widgets.MyLinkPager', array(
                            'pages'                => $pages,
                            'header'               => '',
                            'maxButtonCount'       => 5,
                            'nextPageLabel'        => '&raquo;',
                            'firstPageLabel'       => '←',
                            'lastPageLabel'        => '→',
                            'prevPageLabel'        => '&laquo;',
                            'selectedPageCssClass' => 'active',
                        ))
                        ?>
                    </div>
                </div>

            </div>
        </div><!--/span-->
    </div><!--/row-->
    <script type="text/javascript">
        $(function() {
            $('#jqChart').jqChart({
                title: {text: 'Summary by Month'},
                animation: {duration: 1},
                shadows: {
                    enabled: true
                },
                series: [
                    {
                        type: 'column',
                        title: 'Total Order',
                        fillStyle: '#418CF0',
                        data: [<?= $chart_str ?>]
                    },
                ]
            });

            $('#jqChart').bind('tooltipFormat', function(e, data) {
                return "<b>" + data.x + "</b><br />" +
                        "Orders: " + data.dataItem[2] + "<br />" +
                        "Total: $" + data.y + "";
            });

            $('#btn-clear').bind('click', function() {
                $('#business').val(0);
                $('#staff').val(0);
                $('#le_start_date').val('');
                $('#le_end_date').val('');
                $('#btn-filter-submit').click();
            });
            $('#list_events').perfectScrollbar();

            $('#list_events').scroll(function() {
                $('#tbl_header2').css('top', $(this).scrollTop() + 'px');
                if ($(this).scrollTop() > 70)
                {
                    $('#tbl_header').css('top', $(this).scrollTop() + 'px');
                    $('#tbl_header').show();
                } else {
                    $('#tbl_header').hide();
                }
                if ($(this).scrollLeft() >= 70) {
                    $('#tbl_header2').show();
                    $('#tbl_header2').css('left', $(this).scrollLeft() + 'px');
                    $('#tbl_date_col').css('left', $(this).scrollLeft() + 'px');
                    $('#tbl_date_col').show();
                } else {
                    $('#tbl_header2').hide();
                    $('#tbl_date_col').hide();
                }
            });
            $('.view_row').bind('hover', function() {
                $('.view_row2').css('background-color', '#FFF;');
                $('.view_row2.active').css('background-color', '#FFFFCC;');
                $('.' + $(this).attr('data-rel') + '_td').css('background-color', '#FFFFCC;');
            });
            $('.view_row').bind('click', function() {
                $('.view_row').removeClass('active');
                $('.view_row2').removeClass('active');
                $('.view_row2').css('background-color', '');
                $(this).addClass('active');
                $('.' + $(this).attr('data-rel') + '_td').addClass('active');
            });

        });
        $(document).ready(function() {
<?php if ($date_col != ""): ?>
                $('#tbl_date_col').html('<?= $date_col ?>');
                $('#tbl_date_col').css('top', $('#header_tr').height() + 'px');
                $('#tbl_header2 tr').height($('#header_tr').height());
                $('.view_row').each(function(index, element) {
                    var height = $(this).innerHeight();
                    //console.log($(this).attr('data-rel')+'_td  '+ height);
                    $('.' + $(this).attr('data-rel') + '_td').height(height);
                });
                totalWidth = 0;
                width = $('.col_date').outerWidth();
                totalWidth += width;
                //console.log(width);
                if (width > 0) {
                    $('.col_date_td').css('min-width', (width - 16) + 'px !important');
                    $('.col_date_td').attr('style', 'width: ' + (width - 16) + 'px !important;border-bottom-right-radius:0px;webkit-border-bottom-right-radius: 0px');
                } else {
                    $('.col_date_td').css('min-width', 90 + 'px !important');
                    $('.col_date_td').attr('style', 'width: ' + 90 + 'px !important;border-bottom-right-radius:0px;webkit-border-bottom-right-radius: 0px');
                }
                width = $('.col_apt_date').outerWidth();
                totalWidth += width;
                //console.log(width);
                if (width > 0) {
                    $('.col_apt_date_td').css('min-width', (width - 16) + 'px !important');
                    $('.col_apt_date_td').attr('style', 'width: ' + (width - 16) + 'px !important;border-bottom-right-radius:0px;webkit-border-bottom-right-radius: 0px');
                } else {
                    $('.col_apt_date_td').css('min-width', 90 + 'px !important');
                    $('.col_apt_date_td').attr('style', 'width: ' + 90 + 'px !important;border-bottom-right-radius:0px;webkit-border-bottom-right-radius: 0px');
                }
                $('#tbl_header2').width(totalWidth);
                $('#tbl_date_col').width(totalWidth + 1);

<?php endif; ?>
            var col_set = 0;
            $('#header_tr th').each(function(index, element) {
                col_set++;
                var col_set2 = 0;
                var width = $(this).width();
                $('#tbl_header th').each(function(index, element) {
                    col_set2++;
                    if (col_set == col_set2) {
                        $(this).attr('style', 'width:' + width + 'px !important;min-width:' + width + 'px !important');
                        console.log(width);
                        return false;
                    }
                });
            });
        });
        $(window).load(function() {
<?php if ($date_col != ""): ?>
                $('.view_row').each(function(index, element) {
                    var height = $(this).innerHeight();
                    //console.log($(this).attr('data-rel')+'_td  '+ height);
                    $('.' + $(this).attr('data-rel') + '_td').height(height);
                });
<?php endif; ?>
            var col_set = 0;
            $('#header_tr th').each(function(index, element) {
                col_set++;
                var col_set2 = 0;
                var width = $(this).width();
                $('#tbl_header th').each(function(index, element) {
                    col_set2++;
                    if (col_set == col_set2) {
                        $(this).attr('style', 'width:' + width + 'px !important;min-width:' + width + 'px !important');
                        console.log(width);
                        return false;
                    }
                });
            });
        });
        $('#export_form1').click(function() {
            $('#biz_ukey').attr('value', $('#business').val());
            $('#staff_ukey').attr('value', $('#staff').val());

            var _start_date = $('#le_start_date').val();
            if (_start_date === '') {
                _start_date = '0';
            }
            $('#start_date').attr('value', _start_date);


            var _end_date = $('#le_end_date').val();
            if (_end_date === '') {
                _end_date = '0';
            }

            $('#end_date').attr('value', _end_date);

            $('#frm_export').submit();
        });

        $('#btnprint').click(function() {
            printDiv('Table_event');
        });

        $('#le_start_date, #le_end_date').datepicker({
            dateFormat: "dd-mm-yy"
        });
        //hide popover
        $(':not(#anything)').on('click', function(e) {
            $('.pop-over').each(function() {
                //the 'is' for buttons that trigger popups
                //the 'has' for icons and other elements within a button that triggers a popup
                if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
                    $(this).popover('hide');
                    return;
                }
            });
        });
<?php if (!$this->isStaff): ?>
            load_list_business();

            // get all business
            var listBusiness = [];
            function load_list_business() {
                $.post('<?php echo Yii::app()->createUrl('Appointment/GetBiz'); ?>', {
                    loadBusinessList: 'yes'
                }, function(data) {
                    if (typeof (data) == 'object') {
                        listBusiness = [];
                        listBusiness = data;
                        show_select_bis();
                    } else {
                        alert(data);
                    }
                }, 'json');
                return false;
            }

            var listStaff = [];
            function load_select_staff(ukey) {
                if (ukey == undefined || ukey == "")
                    ukey = $("#business").val();
                $.post('<?php echo Yii::app()->createUrl('Appointment/GetStaffbyBizUkey'); ?>', {
                    loadStaffList: 'yes',
                    biz_ukey: ukey,
                }, function(data) {
                    if (typeof (data) == 'object') {
                        listStaff = [];
                        listStaff = data;
                        show_select_staff();
                    } else {
                        alert(data);
                    }
                }, 'json');
                return false;
            }

            function show_select_staff() {
                var str = '';
                var staff_u = '<?= (isset($select_staff) && $select_staff != "") ? $select_staff : 0 ?>';
                if (listStaff.length > 0) {
                    str += '	<option value="0">Select Staff</option>';
                    for (var i = 0; i < listStaff.length; i++) {
                        if (staff_u != "" && staff_u !== 0 && staff_u == listStaff[i].staff_ukey)
                            str += '<option value="' + listStaff[i].staff_ukey + '">' + listStaff[i].staff_name + '</option>';
                        else
                            str += '<option value="' + listStaff[i].staff_ukey + '">' + listStaff[i].staff_name + '</option>';
                    }
                } else {
                    str += '	<option value="0">Select Staff</option>';
                }
                $("#staff").removeAttr('disabled');
                $("#staff").empty().append(str);
                $("#staff").val(staff_u);
            }

            function show_select_bis() {
                var selectedV = '<?= $select_merchant ?>';
                var str = '';
                //str += '<select data-rel="chosen" id="business_filter" onchange="load_select_staff(); load_events(1)" name="business">';
                str += '	<option value="0">Select Merchant</option>';
                if (listBusiness.length > 0) {
                    for (var i = 0; i < listBusiness.length; i++) {
                        if (selectedV != "" && selectedV == listBusiness[i].biz_ukey)
                            str += '<option selected=selected value="' + listBusiness[i].biz_ukey + '">' + listBusiness[i].biz_name + '</option>';
                        else
                            str += '<option value="' + listBusiness[i].biz_ukey + '">' + listBusiness[i].biz_name + '</option>';
                    }
                }
                $('#business').html(str);
            }
<?php endif; ?>
        function load_events(type) { //1: onchange bis, 2:onchange staff
            var staff_fil = $("#staff_filter").val() == null ? 0 : $("#staff_filter").val();
            var staff_ukey = (type == 1) ? 0 : staff_fil;
            var start_date = ($('#le_start_date').val() == '') ? 0 : $('#le_start_date').val();
            var end_date = ($('#le_end_date').val() == '') ? 0 : $('#le_end_date').val();
            $.ajax({
                url: '<?php echo Yii::app()->createUrl('Report/GetOrdersByStaff') ?>',
                data: {
                    load_events: 'yes',
                    biz_ukey: $("#business_filter").val(),
                    staff_ukey: staff_ukey,
                    start_date: start_date,
                    end_date: end_date
                },
                success: function(data) {
                    $('#list_events').html(data);
                }
            });
            return false;
        }
        $(function() {
<?php if (isset($select_merchant) && !empty($select_merchant)): ?>
                load_select_staff('<?= $select_merchant ?>');
<?php endif; ?>
        });
        //    $('.pop-over').popover();
    </script>
    <script src="<?php echo $this->assetDirWeb ?>js/Webmaster/custom.js"></script>
    <script src="<?php echo $this->assetDirWeb ?>js/Webmaster/jquery.print.js"></script>
